home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-04-28 | 5.0 KB | 171 lines | [TEXT/KAHL] |
- // Bolo code (C) Stuart Cheshire <cheshire@cs.stanford.edu> 1987-1995.
- // All rights reserved. This code is owned by Stuart Cheshire and is donated
- // free of charge for non-commercial use. You may not use this code in any
- // product sold for commercial profit, except shareware priced at $25 or less.
-
- #include <TextUtils.h> // For EqualString
- #include <Errors.h> // for userCanceledErr
-
- #include "BrainFrame.h"
- #include "BF_Globals.h"
- #include "BF_Utils.h"
- #include "BF_IAC.h"
-
- local PPCPortRefNum BoloPPCPort;
-
- export OSErr CallBolo(ConnectionInfo *conn, BoloCommand command)
- {
- OSErr retcode;
- BoloRequest req;
- PPCParamBlockRec p;
-
- req.sig = 'BOLO';
- req.version = BoloMessageVersion;
- req.command = command;
-
- p.readParam.ioCompletion = NULL;
- p.readParam.sessRefNum = conn->BoloSessRefNum;
- p.readParam.bufferLength = sizeof(req);
- p.readParam.bufferPtr = (Ptr)&req;
- p.readParam.more = FALSE;
- p.readParam.userData = 0;
- p.readParam.blockCreator = 'BOLO';
- p.readParam.blockType = 'BBRN';
- retcode = PPCWrite(&p.writeParam, FALSE);
- //if (retcode) fatal("PPCWrite failed %d", retcode);
- if (retcode) return(retcode);
-
- if (command == BoloView) // Only BoloView expects a reply
- {
- p.readParam.bufferLength = sizeof(conn->info);
- p.readParam.bufferPtr = (Ptr)&conn->info;
- retcode = PPCRead(&p.readParam, FALSE);
- //if (retcode) fatal("PPCRead failed %d", retcode);
- }
-
- return(retcode);
- }
-
- local OSErr MyPPCOpen(PPCPortRefNum *thePortRefNum, Boolean *nbpRegisteredFlag)
- {
- static const u_char myname[] = "\pBrainFrame";
- OSErr retcode;
- PPCPortRec thePPCPortRec = { smRoman, "\p", ppcByString, "\pBrainFrame 0" };
- PPCOpenPBRec thePPCOpenPBRec;
-
- BlockMove(myname, thePPCPortRec.name, 1+myname[0]);
-
- thePPCOpenPBRec.serviceType = ppcServiceRealTime;
- thePPCOpenPBRec.resFlag = 0;
- thePPCOpenPBRec.portName = &thePPCPortRec;
- thePPCOpenPBRec.locationName = NULL;
- thePPCOpenPBRec.networkVisible = TRUE;
-
- do {
- thePPCPortRec.u.portTypeStr[thePPCPortRec.u.portTypeStr[0]]++;
- retcode = PPCOpen(&thePPCOpenPBRec, FALSE);
- } while (retcode == portNameExistsErr);
-
- if (retcode == noErr)
- {
- *thePortRefNum = thePPCOpenPBRec.portRefNum;
- *nbpRegisteredFlag = thePPCOpenPBRec.nbpRegistered;
- }
- return(retcode);
- }
-
- local OSErr MyStartSession(PPCPortRefNum port, PortInfoRec *portInfo, LocationNameRec *theLocation,
- PPCSessRefNum *sessionRef, unsigned long *userRef)
- {
- OSErr retcode;
- PPCStartPBRec thePPCStartPBRec;
- thePPCStartPBRec.ioCompletion = NULL;
- thePPCStartPBRec.portRefNum = port;
- thePPCStartPBRec.serviceType = ppcServiceRealTime;
- thePPCStartPBRec.resFlag = 0;
- thePPCStartPBRec.portName = &portInfo->name;
- thePPCStartPBRec.locationName = theLocation;
- thePPCStartPBRec.userData = 0;
- thePPCStartPBRec.userRefNum = 0;
- if (!portInfo->authRequired) retcode = PPCStart(&thePPCStartPBRec, FALSE);
- else
- {
- Boolean guestselected;
- Str32 username = "\p";
- retcode = StartSecureSession(&thePPCStartPBRec, username, TRUE, FALSE, &guestselected, NULL);
- }
- if (retcode == noErr)
- {
- *sessionRef = thePPCStartPBRec.sessRefNum;
- *userRef = thePPCStartPBRec.userRefNum;
- }
- return(retcode);
- }
-
- local OSErr MyEndSession(PPCSessRefNum session)
- {
- PPCEndPBRec theEndPBRec;
- theEndPBRec.sessRefNum = session;
- return(PPCEnd(&theEndPBRec, FALSE));
- }
-
- local OSErr MyDeleteNewUserRefNum(unsigned long userRef)
- {
- if (userRef != 0)
- {
- unsigned long defUserRef;
- Str32 defUserName;
- // If there is no default user, or there is a default user but we are not it,
- // then we should delete our userRef
- if (GetDefaultUser(&defUserRef, defUserName) || defUserRef != userRef)
- return(DeleteUserIdentity(userRef));
- }
- return(noErr);
- }
-
- local pascal Boolean BrowseFilter(LocationNamePtr theLoc, PortInfoPtr thePortInfo)
- {
- theLoc; // Unused
- return(thePortInfo->name.portKindSelector == ppcByString &&
- EqualString(thePortInfo->name.u.portTypeStr, "\pBoloTank", TRUE, TRUE));
- }
-
- export OSErr PPCToolBoxConnect(ConnectionInfo *conn)
- {
- LocationNameRec theLocation;
- PortInfoRec portInfo;
- OSErr retcode;
- retcode = PPCBrowser("\pSelect a Bolo tank to view from", "\pTanks",
- FALSE, &theLocation, &portInfo, BrowseFilter, NULL);
- if (retcode && retcode != userCanceledErr) fatal("PPCBrowser failed %d", retcode);
- if (retcode) return(retcode);
-
- conn->portinfo = portInfo.name;
-
- retcode = MyStartSession(BoloPPCPort, &portInfo, &theLocation, &conn->BoloSessRefNum, &conn->BoloUserRefNum);
- if (retcode) fatal("MyStartSession failed %d", retcode);
-
- return(retcode);
- }
-
- export void PPCToolBoxDisconnect(ConnectionInfo *conn)
- {
- CallBolo(conn, BoloSuspend);
- if (conn->BoloSessRefNum) MyEndSession(conn->BoloSessRefNum);
- if (conn->BoloUserRefNum) MyDeleteNewUserRefNum(conn->BoloUserRefNum);
- }
-
- export OSErr PPCToolBoxInit(void)
- {
- Boolean nbpRegisteredFlag;
- OSErr retcode = MyPPCOpen(&BoloPPCPort, &nbpRegisteredFlag);
- if (retcode) fatal("MyPPCOpen failed %d", retcode);
- return(retcode);
- }
-
- export void PPCToolBoxQuit(void)
- {
- PPCClosePBRec p;
- if (BoloPPCPort) { p.portRefNum = BoloPPCPort; PPCClose(&p, FALSE); }
- }
-